import axios from 'axios'
import Vue from 'vue'
import router from '../router'
const http = axios.create({
  baseURL: 'http://47.99.118.233:8080/admin/api'
})
http.interceptors.request.use(config=>{
  if(localStorage.token){
    config.headers.Authorization = `Bearer ${localStorage.token}`
  }
  return config
},error=> Promise.reject(error))

http.interceptors.response.use(res=>{
  let data = res.data
  if(!data.success){
    Vue.prototype.$message({
      type: 'error',
      message: data.message || '出现错误'
    })
  }
  if(data.code===401){
    router.push('/login')
  }
  return data
},err => {
  return Promise.reject(err)
})
export default http